package com.gg.registration.hospital.controller;

import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.registration.hospital.service.DepartmentService;
import com.gg.registration.hospital.service.HospitalService;
import com.gg.registration.hospital.service.HospitalSetService;
import com.gg.registration.hospital.service.ScheduleService;
import com.gg.registration.model.hosp.Hospital;
import com.gg.registration.model.hosp.Schedule;
import com.gg.registration.vo.hosp.DepartmentVo;
import com.gg.registration.vo.hosp.HospitalQueryVo;
import com.gg.registration.vo.hosp.ScheduleOrderVo;
import com.gg.registration.vo.order.SignInfoVo;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chenjunjia
 * @date 2022/4/10 19:38
 */
@RestController
//@CrossOrigin
@RequestMapping("/admin/hospital")
public class HospitalController {

    @Resource
    private HospitalService hospitalService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    @Resource
    private HospitalSetService hospitalSetService;

    /**
     * 条件分页查询医院列表
     * @param currentPage 当前页
     * @return 统一返回结果集
     *
     * 这里改成POST了注意一下，之前是get，前端报错的话记得从前端get改成post
     */
    @PostMapping("getList/{currentPage}/{limit}")
    public CommonResult getList(@PathVariable("currentPage") Integer currentPage,
                                @PathVariable("limit") Integer limit,
                                @RequestBody HospitalQueryVo hospitalQueryVo) {
        Page<Hospital> page = hospitalService.getList(currentPage,limit,hospitalQueryVo);
        page.getContent();
        return new CommonResult(ResultCodeEnum.SUCCESS,page);
    }

    /**
     * 设置医院状态
     * @param id MongoDB中的医院id字段应该是String类型的
     * @return 统一返回结果集
     */
    @GetMapping("updateStatus/{id}/{status}")
    public CommonResult updateStatus(@PathVariable("id") String id,
                                     @PathVariable("status") Integer status){
        hospitalService.updateStatus(id,status);
        return new CommonResult(ResultCodeEnum.SUCCESS);
    }

    /**
     * 显示医院详情
     * @param id 医院id
     * @return 统一返回结果集
     */
    @GetMapping("showDetail/{id}")
    public CommonResult showDetail(@PathVariable("id") String id){
        Hospital hospital = hospitalService.getHospitalById(id);
        return new CommonResult(ResultCodeEnum.SUCCESS,hospital);
    }

    /**
     * 根据医院名称模糊查询医院
     * @param hosname 医院名称
     * @return 统一返回结果集
     */
    @GetMapping("findByHosname/{hosname}")
    public CommonResult findByHosname(@PathVariable("hosname") String hosname) {
        List<Hospital> hospitalList = hospitalService.findByHosname(hosname);
        return new CommonResult(ResultCodeEnum.SUCCESS,hospitalList);
    }

    //获取科室列表
    @GetMapping("findDepartment/{hoscode}")
    public CommonResult findDepartment(@PathVariable("hoscode") String hoscode) {
        List<DepartmentVo> departmentTree = departmentService.getDepartmentTree(hoscode);
        return new CommonResult(ResultCodeEnum.SUCCESS,departmentTree);
    }

    /**
     * 获取医院详情和预约详情
     * @param hoscode 医院编号
     * @return 统一返回结果集
     */
    @GetMapping("findHospDetail/{hoscode}")
    public CommonResult findHospDetail(@PathVariable("hoscode") String hoscode) {
        Map<String, Object> item = hospitalService.item(hoscode);
        return new CommonResult(ResultCodeEnum.SUCCESS,item);
    }

    /**
     * 分页查询预约规则详情
     * @param page
     * @param limit
     * @param hoscode
     * @param depcode
     * @return
     */
    @GetMapping("auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public CommonResult getBookingSchedule(@PathVariable Integer page,
                                     @PathVariable Integer limit,
                                     @PathVariable String hoscode,
                                     @PathVariable String depcode) {
        Map<String, Object> bookingScheduleRuleMap = scheduleService.getBookingScheduleRule(page, limit, hoscode, depcode);
        return new CommonResult(ResultCodeEnum.SUCCESS,bookingScheduleRuleMap);
    }

    /**
     * 获取排班数据
     * 【医院接口】
     * @param hoscode
     * @param depcode
     * @param workDate 日期
     * @return
     */
    @GetMapping("auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    public CommonResult findScheduleList(@PathVariable String hoscode,
                                   @PathVariable String depcode,
                                   @PathVariable String workDate) {
        List<Schedule> scheduleDetailList = scheduleService.getScheduleDetail(hoscode, depcode, workDate);
        return new CommonResult(ResultCodeEnum.SUCCESS,scheduleDetailList);
    }

    /**
     * 根据排班id获取排班数据
     * @param scheduleId 排班id
     * @return
     */
    @GetMapping("getSchedule/{scheduleId}")
    public CommonResult getSchedule(@PathVariable String scheduleId) {
        Schedule schedule = scheduleService.getById(scheduleId);
        return new CommonResult(ResultCodeEnum.SUCCESS,schedule);
    }

    /**
     * 订单调用：获取排班详情
     * @param scheduleId 排班id
     * @return
     */
    @GetMapping("inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(@PathVariable("scheduleId") String scheduleId) {
        return scheduleService.getScheduleOrderVo(scheduleId);
    }

    /**
     * 订单调用：获取签名信息
     * @param hoscode 医院编号
     * @return
     */
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(@PathVariable("hoscode") String hoscode) {
        return hospitalSetService.getSignInfoVo(hoscode);
    }

}

